package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.sys.entity.NotifyParm;
import com.dataseek.iot.sys.mapper.NotifyParmMapper;
import com.dataseek.iot.sys.service.NotifyParmService;
import com.dataseek.iot.sys.vo.NotifyParmVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class NotifyParmServiceImpl extends ServiceImpl<NotifyParmMapper, NotifyParm> implements NotifyParmService {


    @Autowired
    private NotifyParmMapper notifyParmMapper;

    @Override
    public NotifyParmVo queryAction(String pramCode, String parmName, Integer num, Integer size) {

        NotifyParmVo notifyParmVo = new NotifyParmVo();
        QueryWrapper<NotifyParm> qw = new QueryWrapper<>();
        //根据参数编码查询参数信息
        if (pramCode != null && !pramCode.isEmpty()) {
            qw.like("parameter_code", pramCode);
        }
        if (parmName != null && !parmName.isEmpty()) {
            if (pramCode != null && !pramCode.isEmpty()) {
                qw.or();
            }
            qw.like("parameter_name", parmName);
        }
            //默认查询0状态的数据
        qw.eq("status", 0);

        IPage<NotifyParm> page = new Page<>(num, size);
        List<NotifyParm> mapList = notifyParmMapper.selectPage(page, qw).getRecords();
        notifyParmVo.setPage(num);
        notifyParmVo.setSize(size);
        notifyParmVo.setTotal(page.getTotal());
        notifyParmVo.setList(mapList);
        return notifyParmVo;
    }

    @Override
    public Integer insertAction(NotifyParm notifyParm) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        int resultCode = 0;
        //根据code查询是否已经存在
        String parmCode = notifyParm.getParameterCode();
        String parmName = notifyParm.getParameterName();
        QueryWrapper<NotifyParm> qw = new QueryWrapper<>();
        qw.eq("parameter_code", parmCode);
        qw.or();
        qw.eq("parameter_name",parmName);
        Integer count = notifyParmMapper.selectCount(qw);
        if (count == 0) {
            notifyParm.setTenantCode(tenantCode);
            if (notifyParm.getCreateBy() == null) {
                notifyParm.setCreateBy("");
                notifyParm.setUpdateBy("");
            }
            this.saveOrUpdate(notifyParm);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateAction(NotifyParm notifyParm) {
        int resultCode = notifyParmMapper.updateById(notifyParm);
        return resultCode;
    }

    @Override
    public Integer deleteAction(List<Integer> ids) {
        int resultCode = notifyParmMapper.deleteBatchIds(ids);
        return resultCode;
    }

}
